import threading
import keyboard
from controller import ApplicationController
from view import ApplicationView
from model import ApplicationModel

def main():
    """主函数，启动宏自动化工具"""
    # 初始化模型
    model = ApplicationModel()
    
    # 先创建视图（不初始化控件）
    view = ApplicationView(None)
    # 再创建控制器
    controller = ApplicationController(view)
    controller.model = model
    # 让视图持有控制器引用
    view.controller = controller

    # 现在再创建界面和菜单栏
    view.create_widgets()
    view.create_menu()
    view.bind_shortcuts()
    
    # 设置全局快捷键F9(运行)和F10(停止)
    def setup_global_hotkeys():
        keyboard.add_hotkey('f9', lambda: controller.start_execution())
        keyboard.add_hotkey('f10', lambda: controller.stop_execution())
    
    hotkey_thread = threading.Thread(target=setup_global_hotkeys, daemon=True)
    hotkey_thread.start()
    
    view.update_recent_menu(model.recent_projects)
    
    try:
        view.mainloop()
    except Exception as e:
        print(f"应用程序发生错误: {str(e)}")

if __name__ == "__main__":
    main()